<?php
/**
*+-------点赞中心操作类------------------------+
*+-------------------------------------------------+
*+--------用户进行视频，赛事的点赞-----------------+
*+-------------------------------------------------+
*+---点赞是type|1对视频，type|2对赛事--------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/

namespace Home\Controller;

// 添加游客操作功能，改为继承HomeController
// by liubo at 2016-05-24
class LikeController extends HomeController {

	/**
	*错误操作信息
	*/
	protected $errInfo = '';

	protected function _initialize(){
		parent::_initialize();
	}
	


	/**
	*用户点赞
	*/
	public function doLike(){
		$data = I('', '', 'intval');
		if(false === $this->checkParam($data))
			$this->ajaxReturn(array('state'=>-1, 'info'=>$this->errInfo));

		// 暂时取消游客点赞功能 liubo 2016-05-27
		/*
		// 游客点赞 by liubo at 2016-05-24
        if ( !UID ) {
            // 上次点赞时间
            $lastLikeTime = cookie('lastLikeTime_'.$data['type'].'_'.$data['id']);
            if (!empty($lastLikeTime)) {
                $this->ajaxReturn(['state' => -3, 'info' => '操作过于频繁，请稍候重试']);
            }
            // 30分钟有效
            cookie('lastLikeTime_'.$data['type'].'_'.$data['id'], time(), 60*30);
        }
        */
        if (!UID) {
        	$this->ajaxReturn(array('status'=>0, 'info'=>'未登录'));
        }

		$eventObj = A('Api/Like', 'Event');
		$res = $eventObj->add($data['type']-1, UID, $data['id']);
		if(!$res){

			$this->ajaxReturn(array('state'=>-2, 'info'=>'已经点赞'));

		}else{

			$res = $this->getLiker($data['id']);

			$this->ajaxReturn(array('state'=>0, 'info'=>'点赞成功', 'data'=>$res));
		}

	}

	/**
	*用户取消点赞
	*/
	public function unLike(){
		if (!UID) {
			$this->ajaxReturn(['state' => -3, 'info' => '游客用户不能取消点赞！']);
		}
		$data = I('', '', 'intval');
		if(false === $this->checkParam($data))
			$this->ajaxReturn(array('state'=>-1, 'info'=>$this->errInfo));

		$eventObj = A('Api/Like', 'Event');
		$res = $eventObj->cancel($data['type']-1, UID, $data['id']);
		if(!$res){

			$this->ajaxReturn(array('state'=>-2, 'info'=>'未点赞不能取消'));

		}else{

			$res = $this->getLiker($data['id']);

			$this->ajaxReturn(array('state'=>0, 'info'=>'取消成功', 'data'=>$res));
		}
	}

	/**
	*获取视频点赞的用户信息
	*含昵称，头像
	*@param int $video 视频标识
	*
	*/
	private function getLiker($video){

		$lEvent = A('Api/Like', 'Event');
        $this->likeUser= $likeUser = $lEvent->getUser($video);
        
        if($likeUser){
            $strLiker = $this->fetch('Template:liker');
            return array('content'=>$strLiker, 'total'=>count($likeUser));

        }else{

        	return array('content'=>'', 'total'=>0);
        }
	}

	/**
	*点赞参数检查
	*/
	private function checkParam($data){

		if(empty($data['id']) || empty($data['type'])){

			$this->errInfo = '参数有误';
			return false;
		}

		if($data['type'] == 1){//视频

			$eventObj = A('Api/Video', 'Event');

		}elseif($data['type'] == 2){//赛事

			$eventObj = A('Api/Match', 'Event');
		}

		$srcData = $eventObj->info($data['id']);
		if (!$srcData){
				$this->errInfo = '视频有误';
				return false;
		}

		return true;

	}

}
